<?php
namespace frontend\controllers;

use common\models\ImageTypeModel;
use common\models\LianTypeModel;
use common\models\LianWebModel;
use common\models\NovelModel;
use common\models\NovelTypeModel;
use common\models\SetAuthorModel;
use frontend\models\SmsCodeModel;
use Yii;
use yii\data\Pagination; //分页
use frontend\base\BaseController;
use frontend\utils\GetUrl;
use frontend\utils\UrlUtil;
use frontend\utils\CurlUtil;

/**
 * Qingdou controller
 */
class QingdouController extends BaseController
{
    public $name = '';
    public $type = '';
    public static $novel_type='';

    //青豆  小说类型
    public function actionGetType()
    {
        $str = file_get_contents('www.qingdoutxt.com');
        $par = '/<a.*href="(.*)"><b>(.*)<\/b><\/a>/Uis';
        preg_match_all($par, $str, $arr);
        echo '<pre>';
        print_r($arr);die;
        $res = [];
        if(!empty($arr)){
            unset($arr[0]);
            $num = count($arr[1]);
            foreach ($arr as $key => $value) {
                foreach ($value as $k => $val) {
                    if($key == 1){
                        $name = 'url';
                    }elseif($key == 2){
                        $name = 'title';
                    }
                    $res[$k][$name] = str_replace('"','',$val);
                }
            }
        }
        if(!empty($res)){
            foreach ($res as $k => $val) {
                $data[] = ['title' => $val['title'], 'source' => 1, 'url' => $val['url'],'time'=>time()];
            }
        }
        $model = NovelTypeModel::tableName();
        $attr = ['title', 'source', 'url','time'];
        $flag = Yii::$app->db->createCommand()->batchInsert($model, $attr, $data)->execute();
        echo $flag;
    }

    public function actionGetNovel()
    {
        ini_set("max_execution_time", "72000");
        $type_list = NovelTypeModel::find()->select(['id','url','title','page'])->where(['source'=>1])->asArray()->all();
        if(!empty($type_list)){
            $flag = true;
            foreach ($type_list as $k => $val) {
                if($flag){
                    for ($i=$val['page'];;$i++){
                        $str = file_get_contents($val['url'].'list-'.$k.'-'.$i.'.html');
                        // $par = '/<a.*title="(.*)".*class="shop".*href="(.*)">.*<div class="shop-info">.*<\/div>.*<div.*class="shop-info">.*(.*).*著.*<\/div>.*<\/a>/Uis';
                        $par = '/<a.*title="(.*)".*class="shop".*href="(.*)">.*<div.*class="shop-info">.*<\/div>.*<div.*class="shop-info">(.*)<\/div>.*<div.*class="shop-info">.*<span.*class="glyphicon.*glyphicon-bullhorn">.*<\/a>/Uis';
                        preg_match_all($par, $str, $arr);
                        if(!empty($arr)){
                            unset($arr[0]);
                            foreach ($arr as $key => $value) {
                                foreach ($value as $num => $v) {
                                    if($key == 1){
                                        $name = 'title';
                                    }elseif($key == 2){
                                        $name = 'url';
                                    }elseif($key == 3){
                                        $name = 'author';
                                    }
                                    $res[$num][$name] = trim(str_replace('"','',$v));
                                }
                            }
                            if(!empty($res)){
                                foreach ($res as $x => $y) {
                                    $data[] = ['title' => $y['title'], 'source' => 1, 'url' => $y['url'], 'author'=>$y['author'], 'type'=>$val['id']];
                                }
                            }
                            $model = NovelModel::tableName();
                            $attr = ['title', 'source', 'url','author','type'];
                            $status = Yii::$app->db->createCommand()->batchInsert($model, $attr, $data)->execute();
                            NovelTypeModel::updateAll(['page'=>$i],['id'=>$val['id']]);
                            usleep(100);
                            echo $status;
                        }else{
                            $flag = false;
                            continue;
                        }
                    }
                }else{
                    continue;
                }
            }
        }
    }

}